package servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.UserInfoDao;
import entity.UserInfo;

/**
 * Servlet implementation class EditUserServlet
 */
@WebServlet("/EditUser")
public class EditUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    // 设置请求编码为 UTF-8
	    request.setCharacterEncoding("UTF-8");
	    
	    // 设置响应编码为 UTF-8
	    response.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=UTF-8");
		
		 UserInfo userInfo  = new UserInfo();
		 UserInfoDao userInfoDao = new UserInfoDao();
		
		 String username = request.getParameter("username");
	     String password = request.getParameter("password");
	     String email = request.getParameter("email");
	     String gender = request.getParameter("gender");
	     
	     try {
	    	 userInfo = (UserInfo) request.getSession().getAttribute("userInfo");
		     userInfo.setEmail(email);
		     userInfo.setGender(gender);
		     userInfo.setPassword(password);
		     userInfo.setUsername(username);
		     userInfoDao.updateUserInfo(userInfo);
		     request.getSession().removeAttribute("userInfo");
		     request.getSession().setAttribute("userInfo", userInfo);
		     response.sendRedirect("userinfo.jsp");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
